//
//  SetOpenTimeView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/26.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SetOpenTimeView: UIView, UITableViewDelegate, UITableViewDataSource, SlecterProcotol {
    var dataSource: SetOpenTimeViewController!
    let cellTage = 1000
    lazy var tableView:UITableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)
    let nameArr = KTimeData
    var selectIndex:NSIndexPath?
    
    
    init(dataSource: SetOpenTimeViewController) {
        super.init(frame: CGRect.zero)
        
        self.dataSource = dataSource
        self.setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpUI() -> Void {
        self.addSubview(tableView)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.clear
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalToSuperview()
        }
    }
    
    
    //MARK: tableViewDelegate
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArr.count
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = BaseCellFactory .createTableViewCellWithModel(model: BaseModel(), tableView: tableView, style: .BaseStyleCell)
        cell.nameLabel.text = (nameArr as NSArray).object(at: indexPath.row) as? String
        cell.nameLabel.font = UIFont.systemFont(ofSize: 12)
        cell.tag = cellTage + indexPath.row
        cell.kindLabel.textAlignment = NSTextAlignment.right
        cell.contentView.addSubview(cell.kindLabel)
        cell.kindLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
            make.left.equalToSuperview().offset(100)
            make.top.bottom.equalToSuperview()
        }
        cell.selectionStyle = .none
        if indexPath.row == 0 {
            cell.kindLabel.text = dataSource.viewModel?.starTime
        }else if indexPath.row == 1 {
            cell.kindLabel.text = dataSource.viewModel?.closeTime
        }else {
            cell.kindLabel.text = dataSource.viewModel?.model.dateStr
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 48
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            let select =  SlecterManager.manager.creatSlecterStyle(style: .timeStyle, title: kText("选择开始时间"), selectStr: dataSource.viewModel?.starTime as AnyObject, dataArr: [], dataSource: self )
           select.pickerShow()
           selectIndex = indexPath as NSIndexPath
            break
        case 1:
            let select =  SlecterManager.manager.creatSlecterStyle(style: .timeStyle, title: kText("选择开始时间"), selectStr: dataSource.viewModel?.closeTime as AnyObject, dataArr: [], dataSource: self )
            select.pickerShow()
            selectIndex = indexPath as NSIndexPath
            break
        case 2:
            let select =  SlecterManager.manager.creatSlecterStyle(style: .dateStyle, title: kText("选择开始时间"), selectStr: dataSource.viewModel?.model.dateArr as AnyObject, dataArr: [], dataSource: self )
            select.pickerShow()
            selectIndex = indexPath as NSIndexPath
            break
        default: break
            
        }
    }
    
    //MARK SlecterProcotol
    func selectData(selecter: AnyObject?) {
        let selecterStr = selecter as? String
        if selectIndex?.row == 0 {
            if  (dataSource.viewModel?.compareTime(starBool: true, timeStr: selecterStr!))! {
                dataSource.viewModel?.starTime = selecterStr!
                let cell = tableView.viewWithTag(cellTage + (selectIndex?.row)!) as! BaseTableViewCell
                cell.kindLabel.text = dataSource.viewModel?.starTime
            }
        }else if selectIndex?.row == 1{
            if (dataSource.viewModel?.compareTime(starBool: false, timeStr: selecterStr!))! {
                dataSource.viewModel?.closeTime = selecterStr!
                let cell = tableView.viewWithTag(cellTage + (selectIndex?.row)!) as! BaseTableViewCell
                cell.kindLabel.text = dataSource.viewModel?.closeTime
            }
        }else {
            
            if selecter != nil {
                dataSource.viewModel?.model.dateArr = selecter as! Array
                dataSource.viewModel?.model.changeDateStr()
                tableView.reloadData()
            }
        }
    
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
